{SysSrv} = require "./SysSrv"
fsAsync = require "fsAsync"

exports.EmpAddSrv = new Class
  Extends: SysSrv
  options:
    tab: "emp"
    $vdts: ["code","password"]
  "@confirmButClk":{isTran:true,comment:"点击确定按钮"}
  confirmButClk: (reqOpt,entry,keyArr,returning,tab)->
    t = this
    o = t.options
    entry.picture = t.uid2buf entry.picture
    rltSet = yield t.saveAddClk reqOpt,entry,keyArr,returning,tab
    yield fsAsync.unlinkAsync entry.picture if entry.picture
    rltSet
  codeVdt: (reqOpt,key,val,id)->
    t = this
    o = t.options
    if val.trim() is ""
      return {err:"编码不能为空!"}
    argArr = []
    sql = "select count(id) count from emp where code=$1"
    argArr.push val
    if o.pgType is "edit"
      argArr.push id
      sql += " and id!=$"+argArr.length
    rltSet = yield t.callSql reqOpt,sql,argArr
    rltSet = rltSet.rows
    if rltSet[0].count > 0
      return {err:"编码 "+val+" 已经存在!"}
    true
  passwordVdt: (reqOpt,key,val)->
    t = this
    o = t.options
    valTrim = val.trim()
    if valTrim is ""
      return {err:"密码不能为空!"}
    if valTrim.length > 12 or valTrim.length < 6
      return {err:"密码必须在6至12个字符之间!"}
    true
